package com.sangmo.fts.api.teacher;

import com.sangmo.boot.framework.api.utils.ApiResponse;
import com.sangmo.boot.framework.common.security.meta.Access;
import com.sangmo.boot.framework.data.filter.PageResult;
import com.sangmo.boot.framework.data.json.ExtViewHelper;
import com.sangmo.boot.framework.data.json.context.ExtViewContext;
import com.sangmo.boot.framework.plugin.dataview.crud.api.BaseViewApi;
import com.sangmo.boot.framework.plugin.dataview.data.DataViewPageSearch;
import com.sangmo.fts.training.interfaces.GradeRequest;
import com.sangmo.fts.training.model.Score;
import com.sangmo.fts.training.service.ScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.HashMap;

@Controller
@RequestMapping("/score/v1")
@Access("score")
public class ScoreApi {

    @Autowired
    private ScoreService scoreService;

    @Access(":read")
    @PostMapping("/search")
    public @ResponseBody
    ApiResponse<PageResult<Score>> search(@RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        ExtViewHelper.setGroup("Score.search", 1);
        return ApiResponse.make(scoreService.search(ps));
    }

    @Access(":grade")
    @PostMapping("/grade")
    public @ResponseBody
    ApiResponse grade(@PathVariable("id")Long id, @RequestBody @Valid GradeRequest req) {
        return ApiResponse.make(scoreService.grade(id, req));
    }

}
